package org.light.domain;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.light.core.Writeable;
import org.light.exception.ValidateException;
import org.light.generator.UtilMethodsGenerator;
import org.light.utils.DomainUtil;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class MtmDaoImpl extends DaoImpl {
	private static final long serialVersionUID = -7376733796370937661L;

	@Override
	public String generateDaoImplString() throws ValidateException{
		return generateEnumDummyDaoImplStatements().getContent();
	}	
	
	public StatementList generateEnumDummyDaoImplStatements() throws ValidateException{
		if (this.domain.getDbType().equalsIgnoreCase("oracle")) {
			this.methods.add(UtilMethodsGenerator.generateInitOracleDbMethod(domain));
		} else {
			this.methods.add(UtilMethodsGenerator.generateInitDbMethod(domain));
		}

		List<Writeable> sList = new ArrayList<Writeable>();
		this.addClassImports("org.springframework.stereotype.Repository");
		this.addClassImports(this.domain.getPackageToken() + "." + this.domain.getDaoSuffix() + "."
				+ this.domain.getStandardName() + "Dao");
		this.addClassImports(this.domain.getPackageToken() + "." + this.domain.getDomainSuffix() + "."
				+ this.domain.getCapFirstDomainNameWithSuffix());
		if (this.packageToken !=null && !"".equalsIgnoreCase(this.packageToken)) sList.add(new Statement(1000L,0,"package "+this.packageToken+"."+this.domain.getDaoimplSuffix()+";"));
		sList.add(new Statement(2000L,0,""));
		long serial = 3000L;
		Set<String> imports = this.generateImportStrings();
		for (String s: imports){
			sList.add(new Statement(serial,0,"import "+s+";"));
			serial += 1000L;
		}
		sList.add(new Statement(serial,0,""));
			
		sList.add(new Statement(serial,0,"@Repository(\""+this.domain.getLowerFirstDomainName()+"DummyDaoImpl\")"));
		StringBuilder sb = new StringBuilder("");
		sb.append("public class ").append(StringUtil.capFirst(this.getDomain().getStandardName())).append("DummyDaoImpl");
		if (extendedType != null) sb.append(" extends ").append(extendedType.getTypeName());
		sb.append(" implements ").append(StringUtil.capFirst(this.getDomain().getStandardName())).append("Dao").append("{");
		sList.add(new Statement(serial+1000L,0,sb.toString()));
		
		sList.add(new Statement(serial+2000L,1,"public static List<"+this.domain.getCapFirstDomainNameWithSuffix()+"> db = initDb();"));
		serial += 3000L;
		
		Iterator it = this.fields.iterator();
		while(it.hasNext()){
			Field f = (Field)it.next();
			sList.add(new Statement(serial+1000L,1,"public "+f.getFieldType()+" "+f.getFieldName()+";"));
		}

		Iterator it2 = this.methods.iterator();
		while(it2.hasNext()){
			Method m = (Method)it2.next();
			sList.add(m.generateMethodFullStatements(serial, 0));
			sList.add(new Statement(serial+1000L,0,""));
			serial += 2000L;
		}
		sList.add(new Statement(serial,0,"}"));
		return WriteableUtil.merge(sList);
	}
}
